package com.example.asus.yuejian_10.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.MainActivity;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.bean.User;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.asus.yuejian_10.Config.IMAGE_URL;

public class UserupdateActivity extends AppCompatActivity {
    private ImageView updata_userpic;
    private TextView updata_uname;
    private TextView updata_password;
    private TextView updata_submit;
    private RetrofitSeveice service;
    private Context context;
    private int uid;
    private String name;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userupdate);
        updata_userpic = (ImageView) findViewById(R.id.updata_userpic);
        updata_uname = (TextView) findViewById(R.id.updata_uname);
        updata_password = (TextView) findViewById(R.id.updata_password);
        updata_submit = (TextView) findViewById(R.id.updata_submit);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        service = retrofit.create(RetrofitSeveice.class);

        MainActivity mainActivity = new MainActivity();
        uid = Integer.parseInt(mainActivity.getUserId());


        Call<User> call = service.getuserinfo(uid);

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                //Toast.makeText(getActivity(), user.getUser_name(), Toast.LENGTH_SHORT).show();

                updata_password.setText(user.getPassword());
                updata_uname.setText(user.getUser_name());

                if(user.getPhoto()!=null){
                    Picasso.with(context)
                            .load(IMAGE_URL+user.getPhoto())
                            .into(updata_userpic);
                }
                else {
                    updata_userpic.setImageResource(R.drawable.diagonallayout_hughjackman);
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(UserupdateActivity.this, "联网失败！！！", Toast.LENGTH_SHORT).show();
            }
        });

        updata_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = String.valueOf(updata_uname.getText());
                password = String.valueOf(updata_password.getText());
                Call<Integer> userCall = service.updateuser(uid,name,password);
                userCall.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Integer number = response.body();
                        if(number>0){
                            Toast.makeText(UserupdateActivity.this, "修改成功！！！", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else {
                            Toast.makeText(UserupdateActivity.this, "修改失败！！！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {
                        Toast.makeText(UserupdateActivity.this, "联网失败！！！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}
